package com.colorful.insuranceproject.claims;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.CityListBean;
import com.colorful.insuranceproject.adapter.PhysicalExaminationAppointmentAdapter;
import com.colorful.insuranceproject.base.CityChooseActivity;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.PhysicalExaminationAppointmentBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.ClaimsRequest;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/10/27.
 * <p>
 * 体检预约
 */
public class PhysicalExaminationAppointmentActivity extends TitleActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private static final int GET_PHYSICAL_EXAMINATION_APPOINTMENT_URL = 0X1;

    public static final String EXTRA_POSITION = "extra_position";

    private static final int REQUEST_CODE = 10000;

    ClaimsRequest claimsRequest;

    @Bind(R.id.lv_show)
    ListView lvShow;

    CityListBean cb;

    TextView city;

    int isChild;

    private PhysicalExaminationAppointmentAdapter adapter;

    private String id;

    @Override
    protected int getContentView() {
        return R.layout.activity_physical_examination_appointment;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.Physical_examination_appointment));
        addRightBtn(cb.getAreaName(), R.id.city);
        city = (TextView) findViewById(R.id.city);
        Drawable drawable = getResources().getDrawable(R.drawable.down_arrow_white);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//这句一定要加
        city.setCompoundDrawablePadding(10);//设置图片和text之间的间距
        city.setCompoundDrawables(drawable, null, null, null);//setCompoundDrawables用来设置图片显示在文本的哪一端
        city.setOnClickListener(this);
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        cb = bundle.getParcelable(EXTRA_POSITION);
    }

    @Override
    protected void onViewInitialized() {
        cb.setNowCity(cb.getAreaName());//当前区域城市
        cb.setNowCityId(cb.getAreaId());//当前区域ID

        claimsRequest = new ClaimsRequest(getHandler());
        adapter = new PhysicalExaminationAppointmentAdapter(null, mActivity);
        lvShow.setAdapter(adapter);
        lvShow.setOnItemClickListener(this);
        getData();
    }

    private void getData() {
        if (claimsRequest != null) {
            if (isChild == 0) {
                id = cb.getNowCityId();
            } else {
                id = cb.getAreaId();
            }
            claimsRequest.getPhysicalExaminationAppointmentData(id, isChild, GET_PHYSICAL_EXAMINATION_APPOINTMENT_URL);
        }
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_PHYSICAL_EXAMINATION_APPOINTMENT_URL:
                if (response.isSuccessful) {
                    adapter.setData((List<PhysicalExaminationAppointmentBean>) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PhysicalExaminationAppointmentBean bean = adapter.getItem(position);
        launchActivity(PhysicalExaminationDetailsActivity.class,
                PhysicalExaminationDetailsActivity.EXTRA_DETAILS, bean);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            cb = data.getParcelableExtra("id");
            isChild = cb.getIsChild();
            if (isChild == 0) {
                city.setText(cb.getNowCity());
            } else {
                city.setText(cb.getAreaName());
            }
            getData();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.city:
                launchActivityForResult(CityChooseActivity.class, REQUEST_CODE, CityChooseActivity.EXTRA_POSITION, cb);
                break;
        }
    }
}
